package com.macrohard.admin.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macrohard.admin.domain.TeacherExcelModel;
import com.macrohard.admin.service.TeacherService;
import com.macrohard.admin.utils.poi.ExcelUtil;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.Teacher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("teacher")
@Validated
@Api(tags = "教师接口")
@RequiredArgsConstructor
public class TeacherController {

	private final TeacherService teacherService;

	@GetMapping
	@ApiOperation("分页获取所有教师")
	public R get(@NotNull Integer current, @NotNull Integer pageSize) {
		Page<Teacher> page = new Page<>(current, pageSize);
		LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
		page = teacherService.page(page, queryWrapper);
		return R.ok("ok", page);
	}

	@GetMapping("names")
	@ApiOperation("获取所有教师名字")
	public R getNames() {
		return R.ok("ok", teacherService.getAllName());
	}

	@GetMapping("name-teacherid")
	@ApiOperation("获取所有教师名字和工号")
	public R getNameAndTeacherId() {
		List<Map<String, String>> res;
		List<Teacher> list = teacherService.getAllNameAndTeacherid();
		res = list.stream().map(it -> {
			Map<String, String> tmpMap = new HashMap<>();
			tmpMap.put("tTeacherid", it.gettTeacherid());
			tmpMap.put("tName", it.gettName());
			return tmpMap;
		}).collect(Collectors.toList());
		return R.ok("ok", res);
	}

	@ApiOperation("根据id获取教师")
	@GetMapping("{id}")
	public R get(@PathVariable Integer id) {
		return R.ok("ok", teacherService.getById(id));
	}

	@ApiOperation("添加教师")
	@PostMapping
	public R add(@RequestBody Teacher teacher) {
		teacher.settPassword(SecureUtil.md5(teacher.gettPassword()));
		return R.wrapEffectResult(teacherService.add(teacher));
	}

	@PutMapping
	@ApiOperation("修改教师")
	public R update(@RequestBody Teacher teacher) {
		return R.wrapEffectResult(teacherService.modify(teacher));
	}

	@ApiOperation("根据id删除教师")
	@DeleteMapping("{id}")
	public R delete(@PathVariable Integer id) {
		return R.wrapEffectResult(teacherService.removeOne(id));
	}

	@ApiOperation("批量删除教师")
	@DeleteMapping
	public R delete(@RequestBody List<Integer> ids) {
		return R.wrapEffectResult(teacherService.removeByIds(ids));
	}

	@GetMapping("excel")
	public void download(HttpServletResponse response) {
		List<TeacherExcelModel> list = teacherService.list().stream().map(TeacherExcelModel::new).collect(Collectors.toList());
		ExcelUtil.exportExcel(list, "教师信息", TeacherExcelModel.class, response);
	}
}
